using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;

/// <summary>
/// Summary description for CallStatusDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CallStatusDB
    {
        public Int64 Insert(CallStatus callstatus)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXCRMCallStatusInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CXCRMID", callstatus.CXCRMId);
            _sPara.Add("@CallTypeId", callstatus.CallTypeId);
            _sPara.Add("@CarResultId", callstatus.CarResultId);
            _sPara.Add("@CarId", callstatus.CarId);
            _sPara.Add("@CustomerId", callstatus.CustomerId);
            _sPara.Add("@AgentId", callstatus.AgentId);
            _sPara.Add("@AgentLeadId", callstatus.AgentLeadId);
            _sPara.Add("@CallFinishType", callstatus.CallFinishType);
            _sPara.Add("@CallStatusTypeID", callstatus.CallStatusTypeID);
            _sPara.Add("@MainCallStatusTypeID", callstatus.MainCallStatusTypeID);

            if (callstatus.CallFinishType == 2)
            {
                _sPara.Add("@CallBackAgentID", callstatus.CallBackAgentID);
                _sPara.Add("@CallBackDateTime", callstatus.CallBackDateTime);
            }
            else
            {
                if (callstatus.MainCallStatusTypeID == 7 && callstatus.CallStatusTypeID == 10)
                {
                    _sPara.Add("@CallBackAgentID", callstatus.CallBackAgentID);
                    _sPara.Add("@CallBackDateTime", callstatus.CallBackDateTime);
                }
                else
                {
                    _sPara.Add("@CallBackDateTime", DBNull.Value);
                    _sPara.Add("@CallBackAgentID", DBNull.Value);
                }
            }
            _sPara.Add("@StartDateTime", callstatus.StartDateTime);
            _sPara.Add("@Remark", callstatus.Remark);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }

        public CallStatus GetCallStatus(Int64 CarId, Int64 CustomerId)
        {
            CallStatus _mycallstatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMCallStatusSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarId", CarId);
            _spara.Add("@CustomerId", CustomerId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mycallstatus = new CallStatus();
                    _mycallstatus = FillDataRecord(_dt, 0);
                }
            }
            return _mycallstatus;
        }

        private CallStatus FillDataRecord(DataTable myDataTable, int RowId)
        {
            CallStatus myCallStatus = new CallStatus();
            myCallStatus.CXCRMId = Convert.ToInt64(myDataTable.Rows[RowId]["CXCRMId"].ToString());
            myCallStatus.CallTypeId = Convert.ToInt32(myDataTable.Rows[RowId]["CallTypeId"].ToString());
            myCallStatus.CarResultId = Convert.ToInt64(myDataTable.Rows[RowId]["CarResultId"].ToString());
            myCallStatus.CarId = Convert.ToInt64(myDataTable.Rows[RowId]["CarId"].ToString());
            myCallStatus.CustomerId = Convert.ToInt64(myDataTable.Rows[RowId]["CustomerId"].ToString());
            myCallStatus.AgentId = Convert.ToInt64(myDataTable.Rows[RowId]["AgentId"].ToString());
            myCallStatus.AgentLeadId = Convert.ToInt64(myDataTable.Rows[RowId]["AgentLeadId"].ToString());
            myCallStatus.CallFinishType = Convert.ToInt16(myDataTable.Rows[RowId]["CallFinishType"].ToString());
            myCallStatus.MainCallStatusTypeID = Convert.ToInt32(myDataTable.Rows[RowId]["MainCallStatusTypeID"].ToString());
            myCallStatus.CallStatusTypeID = Convert.ToInt32(myDataTable.Rows[RowId]["CallStatusTypeID"].ToString());
            if (myCallStatus.CallFinishType == 2 && myDataTable.Rows[RowId]["CallBackDateTime"] != DBNull.Value)
            {
                myCallStatus.CallBackAgentID = Convert.ToInt64(myDataTable.Rows[RowId]["CallBackAgentID"].ToString());
                myCallStatus.CallBackDateTime = Convert.ToDateTime(myDataTable.Rows[RowId]["CallBackDateTime"].ToString());
            }

            if (myDataTable.Rows[RowId]["CallBackDateTime"] != DBNull.Value)
            {
                myCallStatus.CallBackAgentID = Convert.ToInt64(myDataTable.Rows[RowId]["CallBackAgentID"].ToString());
                myCallStatus.CallBackDateTime = Convert.ToDateTime(myDataTable.Rows[RowId]["CallBackDateTime"].ToString());
            }

            myCallStatus.CallDuration = Convert.ToInt16(myDataTable.Rows[RowId]["CallDuration"].ToString());
            myCallStatus.Remark = myDataTable.Rows[RowId]["Remark"].ToString();
            myCallStatus.CreatedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["CreatedDate"].ToString());
            myCallStatus.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCallStatus;
        }

        //Sakthi on 07 Jan 2011 -- Start
        public Int64 InsertThankyou(CallStatus callstatus)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCXCRMCallStatusInsertUpdateSingleItemtoThankyou";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@CXCRMID", callstatus.CXCRMId);
            _sPara.Add("@CallTypeId", callstatus.CallTypeId);
            _sPara.Add("@CarId", callstatus.CarId);
            _sPara.Add("@CustomerId", callstatus.CustomerId);
            _sPara.Add("@AgentId", callstatus.AgentId);
            _sPara.Add("@AgentLeadId", callstatus.AgentLeadId);
            _sPara.Add("@CallFinishType", callstatus.CallFinishType);
            _sPara.Add("@MainCallStatusTypeID", callstatus.MainCallStatusTypeID);
            _sPara.Add("@CallStatusTypeID", callstatus.CallStatusTypeID);
            if (callstatus.CallFinishType == 2)
            {
                _sPara.Add("@CallBackAgentID", callstatus.CallBackAgentID);
                _sPara.Add("@CallBackDateTime", callstatus.CallBackDateTime);
            }
            else
            {
                _sPara.Add("@CallBackDateTime", DBNull.Value);
                _sPara.Add("@CallBackAgentID", DBNull.Value);
            }
            _sPara.Add("@StartDateTime", callstatus.StartDateTime);
            _sPara.Add("@Remark", callstatus.Remark);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }
        
        public CallStatus GetCallStatus(Int64 CarId, Int64 CustomerId, Int32 CallTypeId)
        {
            CallStatus _mycallstatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMCallStatusSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarId", CarId);
            _spara.Add("@CustomerId", CustomerId);
            _spara.Add("@CallTypeId", CallTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mycallstatus = new CallStatus();
                    _mycallstatus = FillDataRecord(_dt, 0);
                }
            }
            return _mycallstatus;
        }

        public CallStatus GetCallStatus(Int64 AgentLeadId, Int32 CallTypeId)
        {
            CallStatus _mycallstatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMCallStatusSelectSingleItemByLeadId";

            SortedList _spara = new SortedList();
            _spara.Add("@AgentLeadId", AgentLeadId);
            _spara.Add("@CallTypeId", CallTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mycallstatus = new CallStatus();
                    _mycallstatus = FillDataRecord(_dt, 0);
                }
            }
            return _mycallstatus;
        }

        public DataSet GetCallStatusDS(Int64 CarId, Int64 CustomerId, Int32 CallTypeId)
        {
            DataSet _dsCallStatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXCRMCallStatusSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@CarId", CarId);
            _spara.Add("@CustomerId", CustomerId);
            _spara.Add("@CallTypeId", CallTypeId);

            using (DataSet _ds = _db.GetDataSet(_spName, _spara))
            {
                if (_ds != null && _ds.Tables[0].Rows.Count > 0)
                {
                    _dsCallStatus = _ds;
                }
            }
            return _dsCallStatus;
        }

        public CallStatus GetCallStatus(Int64 CarId, Int64 CustomerId, Int32 CallTypeId, Int64 ProviderCompanyId)
        {
            CallStatus _mycallstatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXCRMCallStatusSelectSingleItemByProviderCompanyId]";

            SortedList _spara = new SortedList();
            _spara.Add("@CarId", CarId);
            _spara.Add("@CustomerId", CustomerId);
            _spara.Add("@CallTypeId", CallTypeId);
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _mycallstatus = new CallStatus();
                    _mycallstatus = FillDataRecord(_dt, 0);
                }
            }
            return _mycallstatus;
        }

        public DataSet GetCallStatusDS(Int64 CarId, Int64 CustomerId, Int32 CallTypeId, Int64 ProviderCompanyId)
        {
            DataSet _dsCallStatus = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocCXCRMCallStatusSelectSingleItemByProviderCompanyId]";

            SortedList _spara = new SortedList();
            _spara.Add("@CarId", CarId);
            _spara.Add("@CustomerId", CustomerId);
            _spara.Add("@CallTypeId", CallTypeId);
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataSet _ds = _db.GetDataSet(_spName, _spara))
            {
                if (_ds != null && _ds.Tables[0].Rows.Count > 0)
                {
                    _dsCallStatus = _ds;
                }
            }
            return _dsCallStatus;
        }
        //Sakthi on 07 Jan 2011 -- End
    }
}